﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.OleDb;

namespace Utility.Database
{
    public class Access
    {
        #region 构造函数
        public Access()
        {
        }
        #endregion

        #region ExecuteSqlCommand
        /// <summary>
        /// Access数据库的Sql命令执行
        /// </summary>
        /// <param name="connectionString">Access数据库的连接字符串</param>
        /// <param name="commandText">数据库执行的Sql命令</param>
        public static void ExecuteSqlCommand(string connectionString, string commandText)
        {
            OleDbConnection oleDbConnection = new OleDbConnection(connectionString);
            OleDbCommand oleDbCommand = new OleDbCommand(commandText, oleDbConnection);
            try
            {
                oleDbConnection.Open();
                oleDbCommand.ExecuteNonQuery();
                oleDbConnection.Close();
            }
            catch (Exception exp)
            {
                throw exp;
            }
        }
        #endregion

        #region ExecuteSqlSelection
        /// <summary>
        /// Access数据库的Sql查询命令执行
        /// </summary>
        /// <param name="connectionString">Access数据库的连接字符串</param>
        /// <param name="selectCommandText">Sql查询命令</param>
        public static DataTable ExecuteSqlSelection(string connectionString, string selectCommandText)
        {
            OleDbConnection oleDbConnection = new OleDbConnection(connectionString);
            OleDbDataAdapter oleDbDataAdapter = new OleDbDataAdapter(selectCommandText, oleDbConnection);
            DataTable dataTableReturn = new DataTable();
            try
            {
                oleDbDataAdapter.Fill(dataTableReturn);
                return dataTableReturn;
            }
            catch (Exception exp)
            {
                throw exp;
            }
        }
        #endregion
    }
}
